#include "mainwindow.h"
#include "ui_mainwindow.h"

/************************decode**********************************/

void MainWindow::timerEvent(QTimerEvent * ev)
{
    if(ev->timerId() == timer_id)
    {
        if(udpSend != nullptr && udpQueue.isEmpty() == false)
        {
            QByteArray recBuf = udpQueue.dequeue();
            networkDecode(recBuf);
        }
    }
}


void MainWindow::networkDecode(QByteArray recBuf)
{
    uint32_t *uPrt =  (uint32_t *)recBuf.data();
    //版本命令
    if(uPrt[0] == DSPQUERY && \
       uPrt[1] == (uint32_t)ui->lineEdit_5->text().toInt() && uPrt[3] == WINGTAIL)
    {
        networkBootloaderVersion(recBuf);
    }
    //传输开始
    else if(uPrt[0] == DSPSTART && \
       uPrt[1] == (uint32_t)ui->lineEdit_5->text().toInt() && uPrt[3] == WINGTAIL)
    {
        networkBootloaderFirst(recBuf);
    }
    //固件传输请求
    else if(uPrt[0] == DSPFIRWARE && \
            uPrt[1] == (uint32_t)ui->lineEdit_5->text().toInt() && uPrt[3] == WINGTAIL)
    {
        networkBootloaderSend(recBuf);
        networkBootloaderStruct.timeout = 0;
        networkBootloaderStruct.timeout_cnt = 0;
    }
}




void MainWindow::networkBootloaderVersion(QByteArray recBuf)
{
    uint32_t *uPrt = (uint32_t *)recBuf.data();
    QString version = "V" + QString::number((uint16_t)((uPrt[5] >> 16)&0xffff))+"."+ \
                          QString::number((uint16_t)(uPrt[5]&0xffff));
       ui->textEdit->append("组件版本号: " + version);
}

void MainWindow::networkBootloaderFirst(QByteArray recBuf)
{

    uint32_t package_len = networkBootloaderStruct.uSubPackageSize;
    uint32_t len = package_len+4;
    uint32_t *uPrt =  (uint32_t *)recBuf.data();
    uint32_t addr = package_len;//首包偏移地址
    if(package_len > 60000) {
        ui->textEdit->append(("固件分包太大,返回..."));
        return;
    }

    //包头
    bootloadSend.uHeader = WINGFIRWARE;
    bootloadSend.uType = uPrt[1];
    bootloadSend.uSize = len;
    bootloadSend.uTail = WINGTAIL;

    bootloadSend.uFirware[0] = (addr>>0)&0xff;
    bootloadSend.uFirware[1] = (addr>>8)&0xff;
    bootloadSend.uFirware[2] = (addr>>16)&0xff;
    bootloadSend.uFirware[3] = (addr>>24)&0xff;
    //第一包数据
    for(uint32_t i=0;i<package_len;i++)
    {
        bootloadSend.uFirware[i+4] = networkBootloaderStruct.uWingsCache[addr + i];
    }

    firwareSendto(&bootloadSend);
    networkBootloaderStruct.uSendLen = addr;
    setProgressBar(networkBootloaderStruct.uSendLen);
}

//void MainWindow::network_bootloader_end(QByteArray recBuf)
//{
//    ui->textEdit->append(("固件更新完成,校验中..."));
//}

void MainWindow::networkBootloaderSend(QByteArray recBuf)
{
    uint32_t package_len = networkBootloaderStruct.uSubPackageSize;//每包大小
    uint32_t *uPrt =  (uint32_t *)recBuf.data();

    uint32_t addr = uPrt[5];//请求的数据包起始地址

    //计算有效数据长度
    uint32_t len = (addr + package_len >= networkBootloaderStruct.uDataSize)?
                    networkBootloaderStruct.uDataSize - addr:package_len;

    if(len > 60000) {
        ui->textEdit->append(("固件分包太大,返回..."));
        return;
    }

    bootloadSend.uHeader = WINGFIRWARE;
    bootloadSend.uType = uPrt[1];
    bootloadSend.uSize = len + 4;
    bootloadSend.uTail = WINGTAIL;

    bootloadSend.uFirware[0] = (addr>>0)&0xff;
    bootloadSend.uFirware[1] = (addr>>8)&0xff;
    bootloadSend.uFirware[2] = (addr>>16)&0xff;
    bootloadSend.uFirware[3] = (addr>>24)&0xff;

    for(uint32_t i=0;i<package_len;i++)
    {
        bootloadSend.uFirware[i+4] = networkBootloaderStruct.uWingsCache[addr + i];
    }

    firwareSendto(&bootloadSend);
    networkBootloaderStruct.uSendLen += len;
    setProgressBar(networkBootloaderStruct.uSendLen);
//    network_write(ui->lineEdit_10->text(),ui->lineEdit_11->text().toInt(),send_message);
//    ui->label_17->setText(QString::number(addr)+"/"+QString::number(networkBootloaderStruct.size));
}
